//
//  CatagoryViewController.m
//  GiftSay
//
//  Created by dllo on 16/1/6.
//  Copyright © 2016年 liyinkai. All rights reserved.
//

#import "CatagoryViewController.h"
#import "HXTactikCustomCollectionCell.h"
#import "HXGiftCollectionCell.h"
#import "HXTactikFirstModel.h"
#import "HXCatagoryFirstModel.h"
#import "HXInsideAllProfessionViewController.h"
#import "CatagoryInsideViewController.h"
#define kTactikUrl @"http://api.liwushuo.com/v2/collections?limit=6&offset=0"
#define kTactikCatagory @"http://api.liwushuo.com/v2/channel_groups/all"

@interface CatagoryViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong) UISegmentedControl *segmentControl;
@property (nonatomic, strong) UICollectionView *catagoryCollection;


@end

@implementation CatagoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    //获取攻略 - 专题数据
    [self getData:kTactikUrl];
    //获取攻略 - 品类数据
    [self getCatagoryData:kTactikCatagory];
    //NavigationSeg
    self.segmentControl = [[UISegmentedControl alloc] initWithItems:@[@"攻略", @"礼物"]];
    self.segmentControl.frame = CGRectMake(self.view.width / 4, 5, self.view.width / 2, 30);
    self.segmentControl.selectedSegmentIndex = 0;
    [self.navigationController.navigationBar addSubview:_segmentControl];
    [self.segmentControl addTarget:self action:@selector(segmentDidPress:) forControlEvents:UIControlEventValueChanged];
    //查看全部专题
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jump) name:@"AllProfession" object:nil];
    //scrollview跳转
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scrollJumpToDetail:) name:@"scrollJumpToDetail" object:nil];
    //item跳转
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemJumpToDetail:) name:@"JumpToInside" object:nil];
}

- (void)search {
    [super search];
    self.segmentControl.hidden = YES;
}

- (void)viewWillAppear:(BOOL)animated {
    self.segmentControl.hidden = NO;
}
//scrollView跳转
- (void)scrollJumpToDetail:(NSNotification *)sender {
    self.segmentControl.hidden = YES;
    HXInsideAllProfessionViewController *insideAllProfessionViewController = [[HXInsideAllProfessionViewController alloc] init];
    
    insideAllProfessionViewController.model = sender.object;
    [self.navigationController pushViewController:insideAllProfessionViewController animated:NO ];
}
//item跳转
- (void)itemJumpToDetail:(NSNotification *)sender {
    self.segmentControl.hidden = YES;
    CatagoryInsideViewController *inside = [[CatagoryInsideViewController alloc] init];
    inside.model = sender.object;
    [self.navigationController pushViewController:inside animated:NO];
    
}
//跳转 全部专题
- (void)jump {
    self.segmentControl.hidden = YES;

        self.allProfession = [[HXAllProfessionViewController alloc] init];

    
    [self.navigationController pushViewController:self.allProfession animated:NO];
    
}

- (void)segmentDidPress:(UISegmentedControl *)sender {
//    NSLog(@"%ld", sender.selectedSegmentIndex);
    if (sender.selectedSegmentIndex == 0) {
        [self.catagoryCollection setContentOffset:CGPointMake(0, 0)];
    } else {
        [self.catagoryCollection setContentOffset:CGPointMake(self.view.width, 0)];
    }
}

//seg对应collectionView
- (void)createView {
//    NSLog(@"qqqqqq%f", self.view.frame.size.);
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(self.view.width, self.view.height - 49);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    
    self.catagoryCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - 49) collectionViewLayout:flowLayout];
    self.catagoryCollection.delegate = self;
    self.catagoryCollection.dataSource = self;
    [self.catagoryCollection registerClass:[HXTactikCustomCollectionCell class] forCellWithReuseIdentifier:@"CatogaryCollection"];
    [self.catagoryCollection registerClass:[HXGiftCollectionCell class] forCellWithReuseIdentifier:@"GiftCollection"];
    self.catagoryCollection.pagingEnabled = YES;
    self.catagoryCollection.showsHorizontalScrollIndicator = NO;
    self.catagoryCollection.bounces = NO;
    [self.view addSubview:_catagoryCollection];
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 2;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.item == 0) {
        HXTactikCustomCollectionCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"CatogaryCollection" forIndexPath:indexPath];
        //属性传值
        item.tactikProfessionArray = self.tactikArray;
        item.tactikCatagoryArray = self.tactikCatagoryArray;

        

        
        self.segmentControl.selectedSegmentIndex = 0;
        item.backgroundColor = [UIColor yellowColor];
        return item;
    } else {
        HXGiftCollectionCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"GiftCollection" forIndexPath:indexPath];
        self.segmentControl.selectedSegmentIndex = 1;
        item.backgroundColor = [UIColor greenColor];
        return item;
    }
    
}
//获取攻略- 专题
- (void)getData:(NSString *)urlStr {
    [LYKNetWorkTool getWithUrl:urlStr parameter:nil httpHeader:nil resposeType:ResponseTypeJSON success:^(id success) {
//        NSLog(@"%@", success);
        NSDictionary *dictionary = success[@"data"];
        NSMutableArray *arr = [NSMutableArray arrayWithCapacity:0];
        for (NSDictionary *dic in dictionary[@"collections"]) {
            HXTactikFirstModel *tactikFirst = [[HXTactikFirstModel alloc] init];
            [tactikFirst setValuesForKeysWithDictionary:dic];
            [arr addObject:tactikFirst];
        }
        self.tactikArray = [NSMutableArray arrayWithArray:arr];
 
    } fail:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}

//获取专题- 品类
- (void)getCatagoryData:(NSString *)urlStr {
    [LYKNetWorkTool getWithUrl:urlStr parameter:nil httpHeader:nil resposeType:ResponseTypeJSON success:^(id success) {
        NSDictionary *dictionary = success[@"data"];
        NSMutableArray *arr = [NSMutableArray arrayWithCapacity:0];
        for (NSDictionary *dic in dictionary[@"channel_groups"]) {
            HXCatagoryFirstModel *firstModel = [[HXCatagoryFirstModel alloc] init];

            [firstModel setValuesForKeysWithDictionary:dic];
            [arr addObject:firstModel];
        }
        self.tactikCatagoryArray = [NSArray arrayWithArray:arr];
        
        
        
        if (self.tactikArray.count > 0 && self.tactikCatagoryArray.count > 0) {
//            NSLog(@"%ld", self.tactikArray.count);
//            NSLog(@"%ld", self.tactikCatagoryArray.count);
            [self createView];
        }
        
        
        
    } fail:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
